const jwt = require('jsonwebtoken');

// 密钥，实际应用中应存储在环境变量中
const SECRET_KEY = 'your-secret-key';

class tokenUtils {
	// 生成token
	genToken(payload) {
		return jwt.sign(payload, SECRET_KEY, { expiresIn: '1h' });
	}
	// 验证token
	verifyToken(token) {
		try {
			let decoded = jwt.verify(token, SECRET_KEY);
			return { valid: true, expired: false, decoded };
		} catch (error) {
			return { valid: false, expired: error.name === 'TokenExpiredError', decoded: null };
		}
	}
	// 解码Token
	decodeToken(token) {
		return jwt.decode(token);
	}
}

module.exports = new tokenUtils();